package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.util.StringUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息;TODO 前端页面无法选择商品类别
     * @return
     */
    // http://localhost:8080/goods/listInventory
    @PostMapping("goods/listInventory")
    public Map<String,Object> listInventory(HttpServletRequest request){
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        String codeOrName = request.getParameter("codeOrName");
        String goodsTypeId = request.getParameter("goodsTypeId");

        Map<String,Object> map = goodsService.listInventory(page,rows,codeOrName,goodsTypeId);
        return map;
    }

    /**
     * 3.2、查询所有商品单位
     */
    @PostMapping("unit/list")
    public Map unitList(){
        return goodsService.unitlist();
    }

    /** 请求URL：http://localhost:8080/goods/list
     * 分页查询商品信息
     * @return Map<String,Object> total rows typename 和salenum
     */
    @PostMapping("goods/list")
    public Map<String,Object> goodsList(HttpServletRequest request){
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String goodsName = request.getParameter("goodsName");
        String goodsTypeId = request.getParameter("goodsTypeId");


        Map<String,Object> map = goodsService.selectGoodsList(page,rows,goodsName,goodsTypeId);
        return map;
    }


    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     3.6、商品添加或修改
     请求URL：http://localhost:8080/goods/save?goodsId=37
     goodsTypeId: 10
     goodsCode: 0001
     goodsName: 陶华碧老干妈香辣脆油辣椒
     goodsModel: 红色装
     goodsUnit: 瓶
     purchasingPrice: 7.95
     sellingPrice: 8.5
     minNum: 1000
     goodsProducer: 贵州省贵阳南明老干妈风味食品有限公司
     remarks: 好卖
     */
    @PostMapping("goods/save")
    public ServiceVO saveGoods(HttpServletRequest request, @RequestParam(value = "goodsId",required = false) String goodsId){
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Goods goods = new Goods();
            BeanUtils.populate(goods,parameterMap);
            if (StringUtil.isNotEmpty(goodsId)){
                goods.setGoodsId(Integer.parseInt(goodsId));
            }
            goodsService.saveOrUpdate(goods);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     3.7、商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
     请求URL：http://localhost:8080/goods/delete

     */
    @PostMapping("goods/delete")
    public ServiceVO deleteGoods(HttpServletRequest request){
        Integer goodsId = Integer.valueOf(request.getParameter("goodsId"));
        return goodsService.deleteGoodsById(goodsId);

    }

    /**
     * 4.1、无库存商品列表展示（可以根据商品名称或编码查询）
     * 请求URL：http://localhost:8080/goods/getNoInventoryQuantity
     * 请求参数：Integer page,Integer rows,String nameOrCode
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("goods/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(HttpServletRequest request){
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String nameOrCode = request.getParameter("nameOrCode");

        Map map =goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        return map;
    }


    /**
     请求URL：http://localhost:8080/goods/getHasInventoryQuantity
     请求参数：Integer page,Integer rows,String nameOrCode
     请求方式：POST
     返回值类型：JSON
     返回值：Map<String,Object>
     */
    @PostMapping("goods/getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(HttpServletRequest request){
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String nameOrCode = request.getParameter("nameOrCode");

        Map map =goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        return map;
    }


    /**
     请求URL：http://localhost:8080/goods/saveStock?goodsId=25
     请求参数：Integer goodsId,Integer inventoryQuantity,double purchasingPrice
     请求方式：POST
     返回值类型：JSON
     返回值：ServiceVO
     Response Example Value：
     {
     "code": 100,
     "msg": "请求成功",
     "info": null
     }
     */
    @PostMapping("goods/saveStock")
    public ServiceVO saveStock(HttpServletRequest request){
        Integer goodsId = Integer.valueOf(request.getParameter("goodsId"));
        Integer inventoryQuantity = Integer.valueOf(request.getParameter("inventoryQuantity"));
        Double purchasingPrice = Double.valueOf(request.getParameter("purchasingPrice"));

        goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    /**
     4.4、删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     请求URL：http://localhost:8080/goods/deleteStock
     请求参数：Integer goodsId
     请求方式：POST
     返回值类型：JSON
     返回值：ServiceVO
     */
    @PostMapping("goods/deleteStock")
    public ServiceVO deleteStock(HttpServletRequest request){
        String goodsId = request.getParameter("goodsId");

        // 不知道是使库存为0，还是删除数据，这里直接删除
        // 若要使其变为0，则state为0时调update把库存设为0
       return goodsService.deleteGoodsById(Integer.valueOf(goodsId));


    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     * 请求URL：http://localhost:8080/goods/listAlarm
     * @return
     */
    @PostMapping("goods/listAlarm")
    public Map<String,Object> listAlarm(){
        Map map = goodsService.listAlarm();
        return map;
    }

}
